﻿package blob.mechanisms
{
    import blob.characters.*;
    import org.flixel.*;

    public class MovableBlock extends FlxSprite implements Mechanism
    {
        private var img:Class;
        private const GRAVITY:int = 1000;
        private const FRICTION:int = 350;
        public var isLocked:Boolean;
        public var beingPushed:Boolean = false;

        public function MovableBlock(param1:Number, param2:Number)
        {
            this.img = MovableBlock_img;
            super(param1, param2);
            loadGraphic(this.img, true, true, 80, 80);
            acceleration.y = 0;
            drag.x = this.FRICTION;
            immovable = true;
            this.Lock();
            return;
        }// end function

        public function activate(param1:Character) : void
        {
            if (param1.size() >= 2 && (isTouching(LEFT) || isTouching(RIGHT)))
            {
                this.beingPushed = true;
            }
            else
            {
                this.beingPushed = false;
            }
            return;
        }// end function

        override public function update() : void
        {
            if (this.beingPushed || !isTouching(FlxObject.FLOOR) && !this.isTouching(UP))
            {
                if (this.isLocked)
                {
                    this.Unlock();
                }
            }
            else if (!this.isLocked && isTouching(FlxObject.FLOOR))
            {
                this.Lock();
            }
            this.beingPushed = false;
            super.update();
            return;
        }// end function

        public function Lock() : void
        {
            this.isLocked = true;
            immovable = true;
            moves = false;
            velocity.x = 0;
            velocity.y = 0;
            acceleration.y = 0;
            return;
        }// end function

        public function Unlock() : void
        {
            this.isLocked = false;
            immovable = false;
            moves = true;
            acceleration.y = this.GRAVITY;
            return;
        }// end function

        public function toggle() : void
        {
            return;
        }// end function

    }
}
